import { Toast } from "antd-mobile";
import { reqcartdelete, reqcartlist, reqcartedit } from "../../http/api";
import { getUserInfo } from "./login";

const initState = {
    cartlist: [],
    allchecked: false,
}

const types = {
    CHANGE_CARTLIST: "SHOP_CHANGE_CARTLIST",
    DEL_SHOP: "SHOP_DEL_SHOP",
    ADD_SHOP: "SHOP_ADD_SHOP",
    SUB_NUM: "SHOP_SUB_NUM",
    /* 全选 */
    ALL_CHECKED: "SHOP_ALL_CHECKED",
    /* 单选 */
    ONE_CHECKED: "SHOP_ONE_CHECKED",
}

const reducer = (state = initState, action) => {
    switch (action.type) {
        case types.CHANGE_CARTLIST:
            return {
                ...state,
                cartlist: [...action.list]
            }
        case types.ALL_CHECKED:
            return {
                ...state,
                allchecked: action.checkall
            }
        default:
            return state
    }
}

export const actions = {

    /* 请求数据 */
    changeCartlist: list => ({ type: types.CHANGE_CARTLIST, list }),
    reqcartlist: () => (dispatch, getState) => {
        let userInfo = getUserInfo(getState());
        reqcartlist({
            uid: userInfo.uid
        }).then(res => {
            if (res.data.code === 200) {
                res.data.list[1].content.forEach((item) => (item.checked = false))
                dispatch(actions.changeCartlist(res.data.list[1].content))
            }
        })
    },

    /* 删除 */
    delShop: (id, index) => (dispatch, getState) => {

        reqcartdelete({ id }).then(res => {
            if (res.data.code === 200) {
                Toast.show(res.data.msg)
                let arr = getCartlist(getState())
                let arr1 = arr.splice(index, 1)
                
                let arr2 = arr.map(item => (item.checked))
                arr.forEach((item, index) => (item.checked = arr[index]));

                let userInfo = getUserInfo(getState());
                reqcartlist({
                    uid: userInfo.uid
                }).then(res => {
                    if (res.data.code === 200) {
                        res.data.list[1].content.forEach((item, index) => (item.checked = arr2[index]))
                        dispatch(actions.changeCartlist(arr))
                    }
                })
            }
            let arr = getCartlist(getState())
            console.log(arr);
        })
    },

    /* 加 */
    addNum: (id) => (dispatch, getState) => {

        reqcartedit({
            id,
            type: 2,
        }).then(res => {
            if (res.data.code === 200) {
                Toast.show(res.data.msg)
            }
            let arr = getCartlist(getState())
            let arr1 = arr.map(item => (item.checked))
            arr.forEach((item, index) => (item.checked = arr1[index]));

            let userInfo = getUserInfo(getState());
            reqcartlist({
                uid: userInfo.uid
            }).then(res => {
                if (res.data.code === 200) {
                    res.data.list[1].content.forEach((item, index) => (item.checked = arr1[index]))
                    dispatch(actions.changeCartlist(res.data.list[1].content))
                }
            })
        })
    },

    /* 减 */
    subNum: (id, num) => (dispatch, getState) => {
        if (num <= 1) {
            num = 1
            return
        }
        reqcartedit({
            id,
            type: 1,
        }).then(res => {
            if (res.data.code === 200) {
                Toast.show(res.data.msg)
            }
            let arr = getCartlist(getState())
            let arr1 = arr.map(item => (item.checked))
            arr.forEach((item, index) => (item.checked = arr1[index]));

            let userInfo = getUserInfo(getState());
            reqcartlist({
                uid: userInfo.uid
            }).then(res => {
                if (res.data.code === 200) {
                    res.data.list[1].content.forEach((item, index) => (item.checked = arr1[index]))
                    dispatch(actions.changeCartlist(res.data.list[1].content))
                }
            })
        })
    },
    changeAllchecked: (checkall) => ({ type: types.ALL_CHECKED, checkall }),

    /* 单选 */
    oneChecked: (onechecked, id) => (dispatch, getState) => {

        let arr = getCartlist(getState());
        arr.forEach(item => item.id === id && (item.checked = onechecked)
        )
        // console.log(arr.every(item => item.checked));
        dispatch(actions.changeAllchecked(arr.every(item => item.checked)))
        dispatch(actions.changeCartlist(arr))
    },
    /* 全选 */
    allChecked: (allchecked) => (dispatch, getState) => {
        let arr = getCartlist(getState());
        arr.forEach(item => (item.checked = allchecked))
        dispatch(actions.changeAllchecked(allchecked))
        dispatch(actions.changeCartlist(arr))
    },


}

export const getCartlist = state => state.Shop.cartlist;
export const checkedall = state => state.Shop.allchecked;
export default reducer;